var express = require('express');
var axios = require('axios');
var router = express.Router();

/**
 * @api {get} /admin/endpagelist 渲染页面
 * @apiName 渲染页面
 * @apiGroup page
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.get('/', (req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, function (req, res, next) {
    if (req.session.identity == "tea") {
        var config = {
            method: 'post',
            baseURL: 'http://localhost:9023',
            url: '/ExamWork/getExamWorkByTeacherAndStatus',
            params: {
                teacherId: req.session.userId,
                status: 1
            },
        }
        axios(config)
            .then(function (response) {
                // console.log(response.data);
                if (response.data.code == 0) {
                    console.log("获取试卷信息成功")
                    res.render('admin/endpagelist', {
                        list: response.data.data
                    });
                } else {
                    // console.log(response);
                    res.send({ err: -1, msg: "获取试卷信息失败" })
                    console.log("获取试卷信息失败")
                }
            })
            .catch(function (error) {
                // console.log(error);
                res.send({ err: -1, msg: "服务器错误，请刷新页面" })
                console.log("服务器错误")
            });
    } else {
        var config = {
            method: 'post',
            baseURL: 'http://localhost:9023',
            url: '/TestWord/getAll',
            params: {
                status: 1
            },
        }
        axios(config)
            .then(function (response) {
                // console.log(response.data);
                if (response.data.code == 0) {
                    console.log("获取试卷信息成功")
                    res.render('admin/adminendpagelist', {
                        list: response.data.data
                    });
                } else {
                    // console.log(response);
                    res.send({ err: -1, msg: "获取试卷信息失败" })
                    console.log("获取试卷信息失败")
                }
            })
            .catch(function (error) {
                // console.log(error);
                res.send({ err: -1, msg: "服务器错误，请刷新页面" })
                console.log("服务器错误")
            });
    }
})



module.exports = router;
